var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // motto: 'Hello World',
    userInfo: null,
    code: null,
    encryptedData: null,
    iv: null,
    energyValue: "",
    number: "",
    step: "",
    steps: "8000",
    bmi: "",
    bmiResult: "",
    body: "",
    bodyResult: "",
    animationAll: null,
    animation1: null,
    animation2: null,
    animation3: null,
    bmiColor: null,
    bodyColor: null,
    userId: null,
    datatime:null
  },
  // 排行榜链接跳转
  bindRankingClick: function () {
    if (this.data.userId) {
      wx.navigateTo({
        url: 'rank/rank?userId=' + this.data.userId 
        + "&number=" + this.data.number
        + "&datatime=" + this.data.datatime
        + "&schoolid=" + this.data.schoolid,
      })
    }
  },
  bindAgain: function () {
    wx.reLaunch({
      url: '../get_mess/get_mess',
    })
  },
  /**   
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //调用应用实例的方法获取全局数据
    wx.getUserInfo({
      success: function (res) {
        that.setData({
          userInfo: res.userInfo
        })
      }
    });
    // 加载动画结束开始
    wx.showLoading({
      title: '加载中',
    })
    wx.login({
      success: function (res) {
        that.setData({ code: res.code })
        if (res.code) {
          wx.getWeRunData({
            success: function (res) {
              that.setData({
                encryptedData: res.encryptedData,
                iv: res.iv
              })
              wx.request({
                url: 'https://woxiaoshitang.com/activity/health.php/applet/index',
                data: {
                  "nickname": that.data.userInfo.nickName,
                  "avatarUrl": that.data.userInfo.avatarUrl,
                  "code": that.data.code,
                  "iv": that.data.iv,
                  "encryptedData": that.data.encryptedData
                },
                method: "POST",
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                success: function (res) {
                  console.log(res)
                  var obj = JSON.parse(res.data.trim())
                  console.log(obj)
                  console.log(obj.time)
                  console.log(obj.schoolid)
                  that.setData({
                    bmi: obj.bmi,
                    body: obj.bodyfat,
                    step: obj.stepnumber,
                    energyValue: obj.quantity,
                    number: obj.number,
                    userId: obj.userid,
                    datatime: obj.time,
                    schoolid: obj.schoolid,
                  })
                  //加载动画结束消失
                  wx.hideLoading()
                  //页面显示
                  var htmlAnimation = wx.createAnimation({
                    duration: 300,
                  })
                  htmlAnimation.opacity(1).step()
                  that.setData({
                    animationAll: htmlAnimation.export(),
                  })
                  //当天步数更新动画
                  var number1 = (Number(that.data.step) / Number(that.data.steps)) * 100 + "%"
                  var stepAnimation = wx.createAnimation({
                    duration: 1600,
                    timingFunction: 'ease',
                    delay: 300,
                  })
                  stepAnimation.width(number1).step()
                  that.setData({
                    animation1: stepAnimation.export(),
                  })
                  //BMI值进度条动画
                  if (Number(that.data.bmi) <= 18.5) {
                    var number2 = (Number(that.data.bmi) / 18.5) * 33
                    that.setData({
                      bmiColor: {
                        color1: "#353535",
                        bmiResult: "偏轻"
                      }
                    })
                  } else if (Number(that.data.bmi) > 18.5 && Number(that.data.bmi) <= 24) {
                    var number2 = Number(that.data.bmi) / 24 * 60
                    that.setData({
                      bmiColor: { color2: "#353535" },
                      bmiResult: "健康"
                    })
                  } else if (Number(that.data.bmi) > 24 && Number(that.data.bmi) <= 28) {
                    var number2 = Number(that.data.bmi) / 28 * 78
                    that.setData({
                      bmiColor: { color3: "#353535" },
                      bmiResult: "肥胖"
                    })
                  } else if (Number(that.data.bmi) > 28 && Number(that.data.bmi) <= 32) {
                    var number2 = Number(that.data.bmi) / 32 * 100
                    that.setData({
                      bmiColor: { color4: "#353535" },
                      bmiResult: "超重"
                    })
                  } else {
                    var number2 = 100
                    that.setData({
                      bmiColor: { color4: "#353535" },
                      bmiResult: "超重"
                    })
                  }
                  var bmiAnimation = wx.createAnimation({
                    duration: 1600,
                    timingFunction: 'ease',
                    delay: 500,
                  })
                  bmiAnimation.width(number2 + "%").step()
                  that.setData({
                    animation2: bmiAnimation.export(),
                  })

                  // 体脂率进度条动画
                  //that.data.number3 是体脂率
                  if (Number(that.data.body) <= 21) {
                    var number3 = (Number(that.data.body) / 21) * 50
                    that.setData({
                      bodyColor: { color1: "#353535" },
                      bodyResult: "瘦"
                    })
                  } else if (Number(that.data.body) > 21 && Number(that.data.body) <= 26) {
                    var number3 = Number(that.data.body) / 26 * 62
                    that.setData({
                      bodyColor: { color2: "#353535" },
                      bodyResult: "偏瘦"
                    })
                  } else if (Number(that.data.body) > 26 && Number(that.data.body) <= 31) {
                    var number3 = Number(that.data.body) / 31 * 74.5
                    that.setData({
                      bodyColor: { color3: "#353535" },
                      bodyResult: "标准"
                    })
                  } else if (Number(that.data.body) > 31 && Number(that.data.body) <= 36) {
                    var number3 = Number(that.data.body) / 36 * 88.8
                    that.setData({
                      bodyColor: { color4: "#353535" },
                      bodyResult: "微胖"
                    })
                  } else if (Number(that.data.body) > 36 && Number(that.data.body) <= 39) {
                    var number3 = Number(that.data.body) / 39 * 100
                    that.setData({
                      bodyColor: { color5: "#353535" },
                      bodyResult: "肥胖"
                    })
                  } else {
                    var number3 = 100
                    that.setData({
                      bodyColor: { color5: "#353535" },
                      bodyResult: "肥胖"
                    })
                  }
                  var bodyAnimation = wx.createAnimation({
                    duration: 1600,
                    timingFunction: 'ease',
                    delay: 700,
                  })
                  bodyAnimation.width(number3 + "%").step()
                  that.setData({
                    animation3: bodyAnimation.export()
                  })
                },
                fail: function (res) {
                  console.log("+++++22222++++++")
                }
              })
            },
            fail: function () {
              console.log("=============")
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
    // bmi:31.6 bodyfat:32 quantity:2160.72 stepnumber:3339 number:1
  },

  /** 
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    setTimeout(function () {

    }.bind(this), 1000)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    setTimeout(function () {

    }.bind(this), 1000)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})